var serviceObj = null;
// Variables for transaction ID:s returned from asyncronous channel calls
var BatteryLevelChannel = "";
var SignalChannel = "";
var NetworkChannel = "";

function initSystemServices(){
    try {
        serviceObj = nokia.device.load("sysinfo");
        GetChannelsOneShot();
    } 
    
    catch (e) {
        var error = e.toString();
        alert(error);
    }
}



/*
 Gets one-time data for a system channels.
 For charging channel which only supports syncronous calling style,
 also display function to show charging status on widget screen is called directly.
 */
function GetChannelsOneShot(){
    try {
        /*
         Get channels and assign callback functions for channels supporting asynchronous calling style
         */
        BatteryLevelChannel = serviceObj.getChannel("BatteryLevel", onBatteryLevelAvailable);
        SignalChannel = serviceObj.getChannel("SignalStrength", onSignalAvailable);
        NetworkChannel = serviceObj.getChannel("Network", onNetworkAvailable);
        // Get charging channel and display initial data (supports only synchronous calling style)
        displayChargingChannel(GetChargingChannel());
    } 
    catch (e) {
        var error = e.toString();
        alert(error);
    }
}

function GetChargingChannel(){
    try {
        var ChargingChannel = serviceObj.getChannel("Charging");
        return ChargingChannel;
    } 
    catch (e) {
        var error = e.toString();
        alert(error);
    }
    
}

/* This function starts all channels supporting notification style 
 * calling.
 */
function StartChannelMonitoring(){
    try {
        BatteryLevelChannel = serviceObj.startChannel(onBatteryLevelNotification, "BatteryLevel");
        SignalChannel = serviceObj.startChannel(onSignalNotification, "SignalStrength");
        NetworkChannel = serviceObj.startChannel(onNetworkNotification, "Network");
    } 
    catch (e) {
        var error = e.toString();
        alert(error);
    }
}

/* This function stops all channels supporting notification style 
 * calling.
 */
function StopChannelMonitoring(){
    try {
        serviceObj.stopChannel("BatteryLevel");
        serviceObj.stopChannel("SignalStrength");
        serviceObj.stopChannel("Network");
    } 
    catch (e) {
        var error = e.toString();
        alert(error);
    }
}


/*
 Callback handler for battery Channel change notifications
 Assigned in sysinfo.startChannel(callback, channel,[trigger])
 Serves outPut object to corresponding display function (in basic.js)
 */
function onBatteryLevelNotification(outPut){

    try {
    
        displayBatterylevelChannel(outPut);
    } 
    catch (e) {
        var error = e.toString();
        alert(error);
    }
}

/*
 Callback handler for battery Channel asyncronous one-time calls
 Assigned in sysinfo.getChannel( channel, [callback])
 Serves outPut object to corresponding display function (in basic.js)
 */
function onBatteryLevelAvailable(outPut){
    try {
        displayBatterylevelChannel(outPut);
    } 
    catch (e) {
        var error = e.toString();
        alert(error);
    }
}

/*
 Callback handler for network Channel change notifications
 Assigned in sysinfo.startChannel(callback, channel,[trigger])
 Serves outPut object to corresponding display function (in basic.js)
 */
function onNetworkNotification(outPut){

    try {
        // Show network data in widget UI
        displayNetworkChannel(outPut);
    } 
    catch (e) {
        var error = e.toString();
        alert(error);
    }
}

/*
 Callback handler for Network Channel asyncronous one-time calls
 Assigned in sysinfo.getChannel( channel, [callback])
 Serves outPut object to corresponding display function (in basic.js)
 */
function onNetworkAvailable(outPut){
    try {
        displayNetworkChannel(outPut);
    } 
    catch (e) {
        var error = e.toString();
        alert(error);
    }
}


/*
 Callback handler for signal Channel asyncronous one-time calls
 Assigned in sysinfo.getChannel( channel, [callback])
 Serves outPut object to corresponding display function (in basic.js)
 */
function onSignalAvailable(outPut){
    try {
        displaySignalChannel(outPut);
    } 
    catch (e) {
        var error = e.toString();
        alert(error);
    }
}

/*
 Callback handler for Signal channel change notifications
 Assigned in sysinfo.startChannel(callback, channel,[trigger])
 Serves outPut object to corresponding display function (in basic.js)
 */
function onSignalNotification(outPut){
    try {
        displaySignalChannel(outPut);
    } 
    catch (e) {
        var error = e.toString();
        alert(error);
    }
}
